﻿using Caliburn.Micro;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using SD.IOC.Core.Mediators;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using SlamDunk.MES.IAppService.DTOs.Inputs;
using SlamDunk.MES.IAppService.DTOs.Outputs.ExecutionContext;
using SlamDunk.MES.IAppService.Interfaces;

namespace SlamDunk.MES.Client.ViewModels.Execution
{
    /// <summary>
    /// 工单弹出层参数录入ViewModel
    /// </summary>
    public class ActualParametersViewModel : ScreenBase
    {
        #region # 字段及构造器
        /// <summary>
        /// 作业管理服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IExecutionContract> _iExecutionContract;

        /// <summary>
        /// 窗口管理器
        /// </summary>
        private readonly IWindowManager _windowManager;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public ActualParametersViewModel(ServiceProxy<IExecutionContract> iExecutionContract, IWindowManager windowManager)
        {
            this._iExecutionContract = iExecutionContract;
            this._windowManager = windowManager;
        }

        #endregion

        #region 属性
        #region 工单参数要求列表
        /// <summary>
        /// 工单参数要求列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<WorkOrderParameterAskInfo> WorkOrderParameterAsks { get; set; }
        #endregion

        #region 工单实际参数列表
        /// <summary>
        /// 工单实际参数列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<WorkOrderActualParameterInfo> WorkOrderActualParameters { get; set; }
        #endregion

        #region 工单Id
        /// <summary>
        /// 工单Id
        /// </summary>
        [DependencyProperty]
        public Guid WorkOrderId { get; set; }
        #endregion

        #region 工厂模型Id
        /// <summary>
        /// 工厂模型Id
        /// </summary>
        [DependencyProperty]
        public Guid? FacilityId { get; set; }
        #endregion
        #endregion

        #region 方法

        #region 加载 —— async Task Load(WorkOrderInfo workOrderInfo)
        /// <summary>
        /// 加载
        /// </summary>
        public async Task Load(WorkOrderInfo workOrderInfo)
        {
            WorkOrderId = workOrderInfo.Id;
            FacilityId = workOrderInfo.FacilityId;
            WorkOrderParameterAsks = new ObservableCollection<WorkOrderParameterAskInfo>(await Task.Run(() => this._iExecutionContract.Channel.GetWorkOrderParameterAsks(
               workOrderInfo.Id)));
            WorkOrderActualParameters = new ObservableCollection<WorkOrderActualParameterInfo>(await Task.Run(() => this._iExecutionContract.Channel.GetWorkOrderActualParameters(
               workOrderInfo.Id)));
        }
        #endregion

        #region 添加参数
        /// <summary>
        /// 添加参数
        /// </summary>
        public async void CreateParameter()
        {
            AddActualParametersViewModel viewModel = ResolveMediator.Resolve<AddActualParametersViewModel>();
            bool? result = await this._windowManager.ShowDialogAsync(viewModel);
            if (result == true)
            {
                WorkOrderActualParameterInfo workOrderActualParameterInfo = new WorkOrderActualParameterInfo();
                workOrderActualParameterInfo.Number = viewModel.ParameterNo;
                workOrderActualParameterInfo.Name = viewModel.ParameterName;
                workOrderActualParameterInfo.RelatedVariableNo = viewModel.RelatedVariableNo;
                workOrderActualParameterInfo.Value = viewModel.Value;
                workOrderActualParameterInfo.ValueUnitNo = viewModel.ValueUnitNo;
                workOrderActualParameterInfo.Sort = viewModel.Sort;
                workOrderActualParameterInfo.Description = viewModel.Description;
                WorkOrderActualParameters.Add(workOrderActualParameterInfo);
            }
        }
        #endregion

        #region 删除工单实际参数
        /// <summary>
        /// 删除工单实际参数
        /// </summary>
        public async void RemoveWorkOrderActualPerson(WorkOrderActualParameterInfo workOrderActualParameterInfo)
        {
            this.Busy();
            //删除工单实际参数
            WorkOrderActualParameters.Remove(workOrderActualParameterInfo);
            this.Idle();
        }
        #endregion



        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证

            #endregion

            List<WorkOrderActualParameterParam> workOrderActualParameterParams = new List<WorkOrderActualParameterParam>();
            foreach (var workOrderActualParameters in WorkOrderActualParameters)
            {
                WorkOrderActualParameterParam workOrderActualParameterParam = new WorkOrderActualParameterParam();
                workOrderActualParameterParam.parameterNo = workOrderActualParameters.Number;
                workOrderActualParameterParam.parameterName = workOrderActualParameters.Name;
                workOrderActualParameterParam.relatedVariableNo = workOrderActualParameters.RelatedVariableNo;
                workOrderActualParameterParam.value = workOrderActualParameters.Value;
                workOrderActualParameterParam.valueUnitNo = workOrderActualParameters.ValueUnitNo;
                workOrderActualParameterParam.sort = workOrderActualParameters.Sort;
                workOrderActualParameterParam.description = workOrderActualParameters.Description;
                workOrderActualParameterParams.Add(workOrderActualParameterParam);
            }
            this.Busy();
            await Task.Run(() => this._iExecutionContract.Channel.SaveWorkOrderActualParameters(
                WorkOrderId, workOrderActualParameterParams));
            await base.TryCloseAsync(true);
            this.Idle();
        }
        #endregion

        #endregion
    }
}
